<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\api\model\ding\SeatImages as SeatImagesModel;
use app\api\model\ding\SeatTag as SeatTagModel;
use app\common\model\ding\Seat as SeatModel;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class Seat extends SeatModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	public static function increased(int $shopId, int $categoryId, array $contentList) {

		$dataset = [];
		foreach ($contentList as $content) {
			$dataset[] = [
				'category_id' => $categoryId,
				'shop_id' => $shopId,
				'name' => $content['name'],
				'people' => $content['people'],
				'store_id' => self::$storeId,
			];
		}
		return (new static )->addAll($dataset);
	}

	public function add($data) {

		return $this->transaction(function () use ($data) {

			
			$data['create_time'] = time();
			$data['store_id'] = self::$storeId;
			$this->save($data);
			$id = $this['seat_id'];
			// 内容
			if (!empty($data['tags'])) {
				SeatTagModel::increased((int) $id, $data['tags']);
			}

			if (!empty($data['uploaded'])) {
				SeatImagesModel::increased((int) $id, $data['uploaded']);
			}
			return true;
		});
	}
	public function edit($data) {

		return $this->transaction(function () use ($data) {

			$this->save($data);
			$id = $this['seat_id'];
			Db::name('ding_seat_tag')->where('seat_id', $id)->delete();
			Db::name('ding_seat_images')->where('seat_id', $id)->delete();
			// 内容
			if (!empty($data['tags'])) {

				SeatTagModel::increased((int) $id, $data['tags']);
			}

			if (!empty($data['uploaded'])) {
				SeatImagesModel::increased((int) $id, $data['uploaded']);
			}
			return true;
		});
	}
	public function remove() {

		$this->delete();

		return true;
	}
	/**
	 * 获取文章列表
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList(int $categoryId = 0, int $shopId) {
		// 检索查询条件
		$filter = [];
		$categoryId > 0 && $filter[] = ['category_id', '=', $categoryId];
		$filter[] = ['shop_id', '=', $shopId];
		// 获取列表数据
		return $this->with(['image', 'tags'])

			->where($filter)
			->where('status',0)
		
			->order(['create_time' => 'desc'])
			->select()->toArray();
	}
	/**
	 * 获取文章列表
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListAll(int $categoryId = 0, int $shopId) {
		// 检索查询条件
		$filter = [];
		$categoryId > 0 && $filter[] = ['category_id', '=', $categoryId];
		$filter[] = ['shop_id', '=', $shopId];
		// 获取列表数据
		return $this->with(['image', 'tags'])

			->where($filter)
		
		
			->order(['create_time' => 'desc'])
			->select()->toArray();
	}
	/*
		*餐位排行
	*/

	public function getListRank(int $shopId = 0, $params) {
		// 检索查询条件
		$filter = [];
		$shopId > 0 && $filter[] = ['shop_id', '=', $shopId];

		if ($params['date'] == 1) {
			$time = time() - 86400 * 7;

		} elseif ($params['date'] == 2) {
			$time = time() - 86400 * 30;

		} else {
			$time = strtotime(Date('Y-m-d'));
		}
		$where[] = ['create_time', '>=', $time];
		// 获取列表数据
		$lists = $this
			->where($filter)

			->select()->toArray();

		foreach ($lists as $key => $order) {
			$lists[$key]['times'] = Db::name('ding_order_seat')
				->where('seat_id', $order['seat_id'])->where($where)->count();

		}

		// 根据 'price' 键的值对数组进行降序排序
		foreach ($lists as $key => $value) {
			if ($value['times'] > 0) {
				$price[$key] = $value['times'];
			}

		}
		$sortedArray = array();
		if (!empty($price)) {
			arsort($price);

			// 重新构造数组，保持 'item' 和 'price' 的关系

			foreach ($price as $key => $value) {
				$sortedArray[] = $lists[$key];
			}
		}

		return $sortedArray;
	}

}